<?php
namespace Home\Controller;
use Home\Controller;
/**
* 文章管理
*/
class ArticleController extends AdminController
{
	/**
	 * 文章首页
	 * @return [type] [description]
	 */
	public function articleManage($order = 'creattime',$p = 1)
	{
		$admininfo = $this->checkLogin();
		$this->assign('admininfo', $admininfo);

		$Article = M('Article');
		$res = $Article
			->field('a.id,b.cate,a.title,a.author,c.username,a.creattime,a.updatetime,a.hits,a.is_home,a.is_top')
			->table(array('bg_article'=>'a','bg_cate'=>'b','bg_admin'=>'c'))
			->where('b.id = a.cateid AND c.id = a.artid')
			->order($order)
			->page($p,10)
			->select();
		$this->assign('res', $res);
		$count = $Article->count();
		$page = new \Think\Page($count,10);
		$page->setConfig('prev','<i class="fa fa-chevron-left"></i>');
		$page->setConfig('next','<i class="fa fa-chevron-right"></i>');
		$show = $page->show();
		$this->assign('page', $show);
		$this->display();
	}
	/**
	 * 添加文章
	 */
	public function addArticle($id = '')
	{
		if (IS_POST) {
			$Article = D('Article');
			if (!$Article->create()) {
				alertMes($Article->getError(), U('article/articleManage'));
			} else {
				$newArt = $_POST;
				$newArt['updatetime'] = time();
				if ($_FILES['artImg']['error'] == 0) {
					$Path = $Article->uploadImg();
					$newArt['bigimg'] = $Path['bigPath'];
					$newArt['smallimg'] = $Path['smallPath'];
				}
				$newArt['artid'] = session('admin.id');
				if ($id != '') {
					$newArt['is_home'] = isset($_POST['is_home']) ? $_POST['is_home'] : 0;
					$newArt['is_top'] = isset($_POST['is_top']) ? $_POST['is_top'] : 0;
					if ($_FILES['artImg']['error'] == 0) {
						$artPath = $Article->where('id=' . $id)->field('bigimg,smallimg')->find();
						unlink($artPath['bigimg']);
						unlink($artPath['smallimg']);
					}
					$res = $Article->where('id=' . $id)->save($newArt);
				} else {
					$newArt['creattime'] = time();
					$res = $Article->add($newArt);
				}
				if ($res) {
					alertMes('成功', U('article/articleManage'));
				} else {
					alertMes('失败', U('article/articleManage'));
				}
			}
		} else {
			$admininfo = $this->checkLogin();
			$this->assign('admininfo', $admininfo);
			//编辑
			if ($id != '') {
				$Article = M('Article');
				$artInfo = $Article->where('a.id='. $id . ' AND b.id=a.cateid')->field('a.id,b.cate,a.title,a.author,a.profiles,a.content,a.is_home,a.is_top,a.bigimg')->table(array('bg_article'=>'a','bg_cate'=>'b'))->find();
				$this->assign('artInfo', $artInfo);
			}
			$cate = M('Cate');
			$res = $cate->field('id,cate')->order('id')->select();
			$this->assign('res', $res);
			$this->display();
		}
	}
	/**
	 * 文章详情页
	 * @param  int $id [description]
	 * @return [type]     [description]
	 */
	public function showArt($id)
	{
		$admininfo = $this->checkLogin();
		$this->assign('admininfo', $admininfo);

		$Article = M('Article');
		$res = $Article->where('a.id=' . $id)->field('a.id,b.cate,a.title,a.author,c.username,a.profiles,a.content,a.creattime,a.updatetime,a.hits,a.is_home,a.is_top,a.bigimg')->table(array('bg_article'=>'a','bg_cate'=>'b','bg_admin'=>'c'))->find();
		$this->assign('res', $res);
		$this->display();
	}
	/**
	 * 删除文章
	 * @param  int $id [description]
	 * @return [type]     [description]
	 */
	public function delArt($id)
	{
		$this->checkLogin();
		$Article = M('Article');
		$artPath = $Article->where('id=' . $id)->field('bigimg,smallimg')->find();
		unlink($artPath['bigimg']);
		unlink($artPath['smallimg']);
		M('Comment')->where('toart=' . $id)->delete();
		$res = $Article->where('id=' . $id)->delete();
		if ($res) {
			alertMes('删除成功', U('article/articleManage'));
		} else {
			alertMes('删除失败', U('article/articleManage'));
		}
	}
	/**
	 * 首页显示
	 * @param  int $id      [description]
	 * @param  int $is_home [description]
	 * @return [type]          [description]
	 */
	public function toHome($id, $is_home)
	{
		$Article = M('Article');
		if ($is_home == 1) {
			$home['is_home'] = 0;
			$res = $Article->where('id=' . $id)->save($home);
			if ($res) {
				alertMes('取消首页显示成功', U('article/articleManage'));
			} else {
				alertMes('取消首页显示失败', U('article/articleManage'));
			}
		}
		if ($is_home == 0) {
			$home['is_home'] = 1;
			$res = $Article->where('id=' . $id)->save($home);
			if ($res) {
				alertMes('首页显示成功', U('article/articleManage'));
			} else {
				alertMes('首页显示失败', U('article/articleManage'));
			}
		}
	}
	public function toTop($id, $is_top)
	{
		$Article = M('Article');
		if ($is_top == 1) {
			$top['is_top'] = 0;
			$res = $Article->where('id=' . $id)->save($top);
			if ($res) {
				alertMes('取消顶置成功', U('article/articleManage'));
			} else {
				alertMes('取消顶置失败', U('article/articleManage'));
			}
		}
		if ($is_top == 0) {
			$top['is_top'] = 1;
			$res = $Article->where('id=' . $id)->save($top);
			if ($res) {
				alertMes('顶置成功', U('article/articleManage'));
			} else {
				alertMes('顶置失败', U('article/articleManage'));
			}
		}
	}
}